﻿using DryIoc;
using DryIoc.Microsoft.DependencyInjection;

using Microsoft.Extensions.DependencyInjection;

using PLCS.App.ConfigManager;
using PLCS.App.EquipmentManager;
using PLCS.App.LogManager;
using PLCS.App.OrderTask;
using PLCS.App.ProcessSolution;
using PLCS.App.Shared;
using PLCS.App.Shared.Service.Dialog;
using PLCS.App.UserAuthorization;
using PLCS.App.ViewModels;
using PLCS.App.ViewModels.Dialogs;
using PLCS.App.Views;
using PLCS.App.Views.Dialogs;
using PLCS.Application.LogShowApp;
using PLCS.Client.IService.LogManager;
using PLCS.Client.IService.UserAuthorization;
using PLCS.Client.Service.LogManager;
using PLCS.Client.Service.UserAuthorization;

using Prism.Modularity;

using System.Windows.Navigation;

using Volo.Abp;

namespace PLCS.App;

/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : PrismApplication
{
    private static System.Threading.Mutex mutex;

    protected override Window CreateShell()
    {
        return Container.Resolve<MainWindow>();
    }

    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        containerRegistry.RegisterDialog<LoginView, LoginViewModel>();
        containerRegistry.RegisterForNavigation<MonitorView, MonitorViewModel>();
        containerRegistry.RegisterForNavigation<StationDetailView, StationDetailViewModel>("StationInfoView");
        containerRegistry.RegisterForNavigation<EquipmentInfoView, EquipmentInfoViewModel>();
        containerRegistry.RegisterForNavigation<ChangePasswordView, ChangePasswordViewModel>();
        containerRegistry.RegisterForNavigation<SettingView, SettingViewModel>();
        containerRegistry.RegisterForNavigation<SendTaskDialogView, SendTaskDialogViewModel>();
        containerRegistry.RegisterForNavigation<RowGoodsDetailView, RowGoodsDetailViewModel>();
        containerRegistry.RegisterForNavigation<ErrorDialogView, ErrorDialogViewModel>();
        containerRegistry.RegisterForNavigation<ModelingView>();

        containerRegistry.Register<IHostDialogService, DialogHostService>();
    }

    protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
    {
        moduleCatalog.AddModule<AppSharedModule>();
        moduleCatalog.AddModule<UserAuthorizationModule>();
        moduleCatalog.AddModule<ProcessSolutionModule>();
        moduleCatalog.AddModule<OrderTaskModule>();
        moduleCatalog.AddModule<EquipmentManagerModule>();
        moduleCatalog.AddModule<ConfigManagerModule>();
        moduleCatalog.AddModule<LogManagerModule>();
    }

    protected override IContainerExtension CreateContainerExtension()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddApplicationAsync<PLCSAppModule>();
        return new DryIocContainerExtension(new Container(CreateContainerRules())
        .WithDependencyInjectionAdapter(serviceCollection));
    }

    protected override void OnInitialized()
    {
        mutex = new System.Threading.Mutex(true, "PLCS.App");
        if (mutex.WaitOne(0, false))
        {
            var dialog = Container.Resolve<IDialogService>();

            dialog.ShowDialog("LoginView", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                base.OnInitialized();
            });
        }
        else
        {
            MessageBox.Show("程序已经在运行！");
            this.Shutdown();
        }
    }

    public static void LoginOut(IContainerProvider containerProvider)
    {
        Current.MainWindow.Hide();

        var dialog = containerProvider.Resolve<IDialogService>();

        dialog.ShowDialog("LoginView", callback =>
        {
            if (callback.Result != ButtonResult.OK)
            {
                Environment.Exit(0);
                return;
            }
            Current.MainWindow.Show();
        });
    }
}